// pages/answerHome/answerHome.js
import {
    shareOptions,
} from "../../utils/access";
import {
    getBarAndTitleHeight,
    jump,
    setStorage,
    getStorage
} from "../../utils/utils";
// 引入动画插件
import lottie from './lottie'
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 信号栏是否有背景
        hasBg: false,
        // 信号栏高度
        ...getBarAndTitleHeight(),
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options?.inviteData) {
            setStorage('inviteData', options?.inviteData)
            console.log('分享人数据：', options.inviteData)
        }
        this.setData({
            ...getBarAndTitleHeight()
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.init()
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },
    /**
     * 初始化加载动画
     */
    init() {
        if (this.inited) {
            return
        }
        wx.createSelectorQuery()
            .selectAll('#face2')
            .node((res) => {
                if (res[0]) {
                    const canvas = res[0].node
                    const context = canvas.getContext('2d')
                    canvas.width = 300
                    canvas.height = 300
                    lottie.setup(canvas)
                    lottie.loadAnimation({
                        loop: true,
                        autoplay: true,
                        path: 'https://image-classify.daieco.com/kejian-quanzi/U6ZZl4mKTlDO69d778d62d2faece8ceaab1493eeb533.json',
                        rendererSettings: {
                            context,
                        },
                    })
                    this.inited = true
                }
            })
            .exec()
        wx.createSelectorQuery()
            .selectAll('#face3')
            .node((res) => {
                if (res[0]) {
                    const canvas = res[0].node
                    const context = canvas.getContext('2d')
                    canvas.width = 300
                    canvas.height = 300
                    lottie.setup(canvas)
                    lottie.loadAnimation({
                        loop: true,
                        autoplay: true,
                        path: 'https://image-classify.daieco.com/kejian-quanzi/65s0rH08WE3G82c2ffcd51d86fc2f1c7923b3a0214ed.json',
                        rendererSettings: {
                            context,
                        },
                    })
                    this.inited = true
                }
            })
            .exec()
        wx.createSelectorQuery()
            .selectAll('#light')
            .node((res) => {
                if (res[0]) {
                    const canvas = res[0].node
                    const context = canvas.getContext('2d')
                    canvas.width = 300
                    canvas.height = 300
                    lottie.setup(canvas)
                    lottie.loadAnimation({
                        loop: true,
                        autoplay: true,
                        path: 'https://image-classify.daieco.com/kejian-quanzi/TPN9dFxLHaUYa14d75b7a6cbf962d838732ce8fc63f4.json',
                        rendererSettings: {
                            context,
                        },
                    })
                    this.inited = true
                }
            })
            .exec()
        wx.createSelectorQuery()
            .selectAll('#note')
            .node((res) => {
                if (res[0]) {
                    const canvas = res[0].node
                    const context = canvas.getContext('2d')
                    canvas.width = 300
                    canvas.height = 300
                    lottie.setup(canvas)
                    lottie.loadAnimation({
                        loop: true,
                        autoplay: true,
                        path: 'https://image-classify.daieco.com/kejian-quanzi/mV439qpefXEx39386f1462f94f629de5e739705123e6.json',
                        rendererSettings: {
                            context,
                        },
                    })
                    this.inited = true
                }
            })
            .exec()
        wx.createSelectorQuery()
            .selectAll('#trophy')
            .node((res) => {
                if (res[0]) {
                    const canvas = res[0].node
                    const context = canvas.getContext('2d')
                    canvas.width = 300
                    canvas.height = 300
                    lottie.setup(canvas)
                    lottie.loadAnimation({
                        loop: true,
                        autoplay: true,
                        path: 'https://image-classify.daieco.com/kejian-quanzi/sq3qUJGP76Sc3e5b5c7bfdaaa969e7acabe1c650fa18.json',
                        rendererSettings: {
                            context,
                        },
                    })
                    this.inited = true
                }
            })
            .exec()
    },

    // 跳转答题pk
    jumpPk() {
        wx.showToast({
            title: '暂未开放',
            icon: 'none'
        })
    },
    // 跳转每日一答
    jumpDay() {
        if (this.loading) return
        this.loading = true
        if (!app.isLogin()) {
            setStorage('redirectPath', '/pages/answerHome/answerHome')
            app.login()
            this.loading = false
            return
        }
        jump('test')
        this.loading = false
    },
    // 跳转连对挑战
    jumpChallenge() {
        if (this.loading) return
        this.loading = true
        if (!app.isLogin()) {
            setStorage('redirectPath', '/pages/answerHome/answerHome')
            app.login()
            this.loading = false
            return
        }
        jump('answerChallenge')
        this.loading = false
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
    // 页面滚动事件
    onPageScroll(e) {
        this.setData({
            hasBg: e?.scrollTop > 20,
        });
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (e) {
        const userId = getStorage('userId') || ''
        return shareOptions('default', e, '全国垃圾分类答题测试', `/pages/answerHome/answerHome?inviteData=${userId}`, null)
    }
})